package com.android.readcard.read;

import android.graphics.Bitmap;
import android.util.Log;

import com.android.readcard.bean.CardData;
import com.android.readcard.bean.ReadCardInfo;
import com.android.readcard.inter.UpLoadListener;
import com.android.readcard.utils.BleGlobUtils;
import com.android.readcard.utils.UploadCardUtils;
import com.kaeridcard.client.BtReaderClient;
import com.kaeridcard.client.IdCardItem;

/**
 * KT8000读卡
 * 2017/8/24
 * 张鹏
 */
public class Kt8000ConnectUtils {

    private String bleAddress;

    private String name, sex, nation, id_num, address, sign_office, to, from, born;
    private String birth_year, birth_month, birth_day, sign_data, photoHeard;
    private Bitmap photo;

    private String bleName;
    private String bluetoothName = "";

    // 注册成功失败监听
    private UpLoadListener mOnUpLoadListener;
    private BtReaderClient mClient;

    private String bluetoothType;

    public Kt8000ConnectUtils(String bluetoothType, String bleName, String bleAddress,
                              final String bluetoothName, final UpLoadListener mOnUpLoadListener) {
        this.bluetoothType = bluetoothType;
        this.bleName = bleName;
        this.bleAddress = bleAddress;
        this.bluetoothName = bluetoothName;
        this.mOnUpLoadListener = mOnUpLoadListener;
    }

    public void connect(final BtReaderClient mClient) {
        this.mClient = mClient;
        boolean connresult = mClient.connectBt(bleAddress);
        if (connresult) {
            // 读卡操作
            readCard();
        } else {
            setErr("连接失败connresult=" + connresult);
        }
    }

    private IdCardItem idcard;

    /**
     * 断开连接
     */
    public void stopConnect() {
        try {
            if (mClient != null) {
                mClient.disconnectBt();
                Log.e("项目异常Kt8000", "------Kt8000断开连接------" + "");
            }
        } catch (Exception e) {
            Log.e("项目异常", "------断开连接异常------" + e.toString());
            setErr("断开连接异常");
            e.printStackTrace();
        }

    }

    private void setErr(String err) {
        mOnUpLoadListener.onErrUpLoad(1, err);
    }

    /**
     * KT8000读出身份证信息
     */
    private void readCard() {
        try {
            idcard = mClient.readIDCard();
        } catch (Exception ex) {
            ex.printStackTrace();
            setErr("读取错误,请重试");
        }
        if (idcard == null) {
            setErr("蓝牙初始化失败请重试,idcard为空");
        } else {
            if (idcard.result_code == 0) {
                photo = idcard.picBitmap;
                photoHeard = BleGlobUtils.replaceBlank(
                        BleGlobUtils.bitmapToBase64(photo));
                name = idcard.name.toString().trim();
                sex = idcard.getSexStr(idcard.sex_code);
                nation = idcard.getNationStr(idcard.nation_code);
                born = idcard.birth_year + idcard.birth_month
                        + idcard.birth_day;
                birth_year = idcard.birth_year;
                birth_month = idcard.birth_month;
                birth_day = idcard.birth_day;

                address = idcard.address;
                id_num = idcard.id_num;
                sign_office = idcard.sign_office;
                to = idcard.useful_e_date_year +
                        idcard.useful_e_date_month +
                        idcard.useful_e_date_day;

                from = idcard.useful_s_date_year +
                        idcard.useful_s_date_month +
                        idcard.useful_s_date_day;
                if (to.contains("长期")) {
                    sign_data = idcard.useful_s_date_year
                            + "." + idcard.useful_s_date_month + "." +
                            idcard.useful_s_date_day + "--" +
                            "长期";
                } else {
                    sign_data = idcard.useful_s_date_year
                            + "." + idcard.useful_s_date_month + "." +
                            idcard.useful_s_date_day + "--" +
                            idcard.useful_e_date_year + "."
                            + idcard.useful_e_date_month + "." +
                            idcard.useful_e_date_day;
                }
                String chuanMa = UploadCardUtils.getChuanMa(bleName, bleAddress);// 获取串码
// 获取身份证数据
                ReadCardInfo cardInfo = UploadCardUtils.getCardInfo(bluetoothName, address, photoHeard, sex, id_num, nation,
                        sign_office, to, name, from, born, sign_data, birth_year,
                        birth_month, birth_day, photo);

                // 获取读卡上传数据,使用时需要把data转换成json和串码一起上传
                CardData cardData = UploadCardUtils.getData(bluetoothType, address, photoHeard, sex, id_num, nation,
                        sign_office, to, name, from, born, sign_data, birth_year,
                        birth_month, birth_day, photo);

                mOnUpLoadListener.onSucUpLoad(cardInfo, cardData, chuanMa);
            } else {
                setErr("读取数据失败,idcard.result_code=" + idcard.result_code);
            }
        }
    }
}
